package service

import (
	"context"
	"database/sql"
	"errors"
	"gf/app/dao"
	"gf/app/model"
	"github.com/gogf/gf/frame/g"
	"strings"
)

var ArticleCategory = articleCategoryService{}

type articleCategoryService struct{}

func (a *articleCategoryService) List(ctx context.Context, pid int) (treeList []model.TreeList, ids []int, err error) {

	var articleCategory model.ArticleCategoryGetListOutput

	err = dao.GfArticleCategory.Ctx(ctx).ScanList(&articleCategory.List, "ArticleCategory")
	if err != nil {
		return nil, nil, err
	}

	var myMenu []model.TreeList
	for _, v := range articleCategory.List {
		list := model.TreeList{ID: int(v.ArticleCategory.Id), ParentID: v.ArticleCategory.Pid, Name: v.ArticleCategory.Name, Level: v.ArticleCategory.Level}
		myMenu = append(myMenu, list)
	}

	treeList, ids = GetMenu(myMenu, pid)

	return treeList, ids, nil
}

func (a *articleCategoryService) Info(ctx context.Context, id int64) (*model.ArticleCategory, error) {

	var articleCategory *model.ArticleCategory

	err := dao.GfArticleCategory.Ctx(ctx).Where("id=? ", id).Scan(&articleCategory)
	if err != nil {
		return articleCategory, err
	}

	return articleCategory, nil
}

func (a *articleCategoryService) Add(ctx context.Context, data *model.ArticleCategoryApiReq) (int64, error) {
	level := 1
	if data.Pid > 0 {
		info, err := a.Info(ctx, int64(data.Pid))
		if err == nil {
			level = info.Level + 1
		}
	}

	id, err := dao.GfArticleCategory.Ctx(ctx).
		Data(g.Map{"name": data.Name, "pid": data.Pid, "level": level}).
		InsertAndGetId()
	if err != nil {
		return 0, err
	}
	return id, nil
}

func (a *articleCategoryService) Del(ctx context.Context, data *model.ArticleCategoryApiInfoReq) (sql.Result, error) {
	_, ids, err := a.List(ctx, data.Id)
	if err != nil {
		return nil, err
	}

	ids = append(ids, data.Id)
	isArticle, err := dao.GfArticle.Ctx(ctx).Where("category_id", ids).Count()
	if err != nil {
		return nil, err
	}

	if isArticle > 0 {
		return nil, errors.New("请先删除分类下的文章")
	}

	result, err := dao.GfArticleCategory.
		Ctx(ctx).
		Where("id=? ", data.Id).
		Delete()
	if err != nil {
		return nil, err
	}
	return result, nil
}

func (a *articleCategoryService) Edit(ctx context.Context, data *model.ArticleCategoryApiSaveReq) (sql.Result, error) {
	result, err := dao.GfArticleCategory.Ctx(ctx).
		Where("id=? ", data.Id).
		Data(g.Map{"name": data.Name}).
		Update()
	if err != nil {
		return nil, err
	}
	return result, nil
}

func GetMenu(menuList []model.TreeList, pid int) ([]model.TreeList, []int) {
	var treeList []model.TreeList
	var ids []int

	for _, v := range menuList {
		if v.ParentID == pid {

			child, treeIds := GetMenu(menuList, v.ID)
			for _, v2 := range treeIds {
				ids = append(ids, v2)
			}

			ids = append(ids, v.ID)

			node := model.TreeList{
				ID:       v.ID,
				Name:     v.Name,
				ParentID: v.ParentID,
				Level:    v.Level,
				Str:      v.Str,
				Children: child,
			}
			node.Str = strings.Repeat("|----", node.Level)
			treeList = append(treeList, node)
		}
	}
	return treeList, ids
}
